package com.zfy.tingshu.common.login;

import com.zfy.tingshu.common.constant.RedisConstant;
import com.zfy.tingshu.common.execption.GuiguException;
import com.zfy.tingshu.common.result.ResultCodeEnum;
import com.zfy.tingshu.common.util.AuthContextHolder;
import com.zfy.tingshu.model.user.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author 86188
 * @version v1.0
 * @project tingshu-parent
 * @Date 2025/9/1 22:16
 * @company []
 * @description
 */
@Component
@Aspect
@Slf4j
public class TsLoginAspect {
    @Autowired
    private RedisTemplate redisTemplate;

    @Around("@annotation(tsLogin)")
    public Object doBasicProfiling(ProceedingJoinPoint proceedingJoinPoint,TsLogin tsLogin) throws Throwable {
        /**
         * 1.先判断当前用户是否登录；登录成功之后，将token方到请求头中；
         *      判断请求头中是否有token就可以了
            2.如果有token
                需要查询缓存中是否真正存在用户信息
         */
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes=(ServletRequestAttributes)requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String token = request.getHeader("token");
        //判断注解的属性
        if (tsLogin.required()){
            //判断token是否为空
            if (StringUtils.isEmpty(token)){
                // 说明没有登录过
                throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
            }
            //判断用户中是否真正有用户信息
            String userLoginKey=RedisConstant.USER_LOGIN_KEY_PREFIX+token;
            //获取缓存中的数据
            UserInfo userInfo = (UserInfo) this.redisTemplate.opsForValue().get(userLoginKey);
            // 如果用户信息为空
            if (null ==userInfo){
                //说明没有登录过；LOGIN_AUTH(208,"未登录");
                throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
            }
        }
        //如果用户信息不为空，就存在缓存中
        if (!StringUtils.isEmpty(token)){
            // 有token，那么缓存中可能存在
            String userLoginKey =RedisConstant.USER_LOGIN_KEY_PREFIX+token;
            //获取缓存中的数据
            UserInfo userInfo = (UserInfo)this.redisTemplate.opsForValue().get(userLoginKey);
            //判断用户对象
            if (null !=userInfo){
                //将用户id存储到本地
                AuthContextHolder.setUserId(userInfo.getId());
            }
        }
        //声明一个对象
        Object proceed;
        try {
            //执行业务方法
            proceed = proceedingJoinPoint.proceed();
        } finally {
            //防止内存泄漏;
            AuthContextHolder.removeUserId();
        }

        return proceed;
    }
}
